package com.tjetc.dao;

import com.tjetc.domain.BossTeam;
import com.tjetc.domain.Team;
import com.tjetc.domain.TeamScore;
import com.tjetc.utils.App;
import com.tjetc.utils.Db;
import org.apache.commons.dbutils.*;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

public class TeamDao {
    private QueryRunner runner=new QueryRunner();
    public Team findByTeamName(String teamName) throws SQLException {
      return Db.runner.query("select * from team where name=?",new BeanHandler<>(Team.class),teamName);

    }

    public void add(Team team,int id) throws SQLException {
        runner.update(Db.getConnection(),"insert into team(id,name,established_date) values (?,?,?)",team.getId(),team.getName(),team.getEstablishedDate());
        runner.update(Db.getConnection(),"insert into boss_team (boss_id,team_id) values (?,?)",id,team.getId());
    }

    public boolean del(Team team) throws SQLException {

        try {
            runner.update(Db.getConnection(),"delete  from team where name=?",team.getName());
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
        return true;

    }


    public List<Team> showAll() throws SQLException {
        QueryRunner queryRunner = new QueryRunner();
        //开启下划线->驼峰转换所用
        BeanProcessor bean = new GenerousBeanProcessor();
        RowProcessor processor = new BasicRowProcessor(bean);
        return Db.runner.query("select * from team",new BeanListHandler<>(Team.class,processor));
    }

    public void update(int teamId, String teamName1, Date date, String teamName) throws SQLException {
        runner.update(Db.getConnection(),"update team set id=?,name=?,established_date=? where name =?",teamId,teamName1,date,teamName);
    }


    public BossTeam findByTeamId(int id) throws SQLException {
        QueryRunner queryRunner = new QueryRunner();
        //开启下划线->驼峰转换所用
        BeanProcessor bean = new GenerousBeanProcessor();
        RowProcessor processor = new BasicRowProcessor(bean);
        return Db.runner.query("select * from boss_team where team_id=?",new BeanHandler<>(BossTeam.class,processor),id);

    }
}
